using System;
using System.Collections.Generic;

namespace PSSymbolStore
{
    internal class RefsFileEntry
    {
        internal static int TRANSACTION_ID_FIELD = 0;
        internal static int FILE_OR_POINTER_FIELD = 1;
        internal static int FILE_PATH_FIELD = 2;
        internal static int SYMBOL_TYPE = 3;
        internal static int IS_COMPRESSED_FIELD = 5;

        public RefsFileEntry()
        {
        }

        public RefsFileEntry(IList<string> fields)
        {
            if (fields == null)
            {
                throw new ArgumentNullException("fields");
            }

            TransactionId = GetField(fields, TRANSACTION_ID_FIELD);
            TransactionUploadType = String.Equals(GetField(fields, FILE_OR_POINTER_FIELD), "ptr",
                                                  StringComparison.InvariantCultureIgnoreCase)
                                        ? TransactionUploadType.Pointer
                                        : TransactionUploadType.File;
            
            FilePath = GetField(fields, FILE_PATH_FIELD);

            if (String.Equals(GetField(fields, SYMBOL_TYPE), "bin", StringComparison.InvariantCultureIgnoreCase))
            {
                SymbolType = SymbolType.Binary;
            }
            else if (String.Equals(GetField(fields, SYMBOL_TYPE), "pri", StringComparison.InvariantCultureIgnoreCase))
            {
                SymbolType = SymbolType.PrivateSymbols;
            }
            else if (String.Equals(GetField(fields, FILE_OR_POINTER_FIELD), "pub", StringComparison.InvariantCultureIgnoreCase))
            {
                SymbolType = SymbolType.PublicSymbols;
            }

            IsCompressed = (String.Equals(GetField(fields, IS_COMPRESSED_FIELD), "Y", StringComparison.InvariantCultureIgnoreCase));
        }

        public string TransactionId { get; set; }
        public TransactionUploadType TransactionUploadType { get; set; }
        public string FilePath { get; set; }
        public bool IsCompressed { get; set; }

        // bin, pri, pub
        public SymbolType SymbolType { get; set; }

        private static string GetField(IList<string> fields, int index)
        {
            if (fields.Count >= index)
            {
                return fields[index];
            }
            return String.Empty;
        }

        public override string ToString()
        {
            string transactionType = String.Empty;
            switch (TransactionUploadType)
            {
                case TransactionUploadType.File:
                    transactionType = "file";
                    break;
                case TransactionUploadType.Pointer:
                    transactionType = "ptr";
                    break;
            }

            string symbolType = String.Empty;
            switch (SymbolType)
            {
                case SymbolType.Binary:
                    symbolType = "bin";
                    break;
                case SymbolType.PrivateSymbols:
                    symbolType = "pri";
                    break;
                case SymbolType.PublicSymbols:
                    symbolType = "pub";
                    break;
            }

            return String.Format("{0},{1},\"{2}\",{3},,{4},,",
                TransactionId, transactionType, FilePath, symbolType,
                (IsCompressed) ? "Y" : "N");
        }
    }
}
